﻿
using System;
using System.Data;
using System.Xml;
using System.Data.OleDb;
using System.Collections;

namespace MiniCMS
{
    public sealed class OleDbHelper
    {
        public static DataSet ExecuteDataset(string connectionString, CommandType commandType, string commandText)
        {
            using (OleDbConnection conn = new OleDbConnection(connectionString))
            {
                OleDbDataAdapter OdbcAda = new OleDbDataAdapter();
                OdbcAda.SelectCommand = new OleDbCommand();
                OdbcAda.SelectCommand.Connection = conn;
                OdbcAda.SelectCommand.CommandType = CommandType.Text;
                OdbcAda.SelectCommand.CommandText = commandText;

                DataSet ds = new DataSet();
                try
                {
                    OdbcAda.Fill(ds);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                return ds;
            }
        }

        public static OleDbDataReader ExecuteReader(string connectionString, CommandType commandType, string commandText)
        {
            try
            {
                OleDbConnection conn = new OleDbConnection(connectionString);
                conn.Open();
                OleDbCommand cmd = new OleDbCommand(commandText, conn);
                cmd.CommandType = CommandType.Text;
                return cmd.ExecuteReader(CommandBehavior.CloseConnection);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static int ExecuteNonQuery(string connectionString, string sql)
        {
            using (OleDbConnection conn = new OleDbConnection(connectionString))
            {
                OleDbCommand cmd = new OleDbCommand(sql, conn);
                cmd.CommandType = CommandType.Text;
                try
                {
                    conn.Open();
                    int value = cmd.ExecuteNonQuery();
                    return value;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
        }
    }
}